/**
 * 
 */
package Consult.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import Consult.model.BookType;
import Consult.model.User;
import Util.DBUtils;
import Util.StringUtil;

import java.awt.Toolkit;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.awt.event.ActionEvent;

/**
 * @author zhaoyana
 *注册页面
 */
public class RegFrm extends JFrame {

	private JPanel contentPane;
	private JTextField userNametxt;
	private JTextField textField_2;
	private JPasswordField passwordtxt;
	private DBUtils dbUtil=new DBUtils();
	private Com.userDao userDao=new Com.userDao();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					RegFrm frame = new RegFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public RegFrm() {
		setTitle("注册新账号");
		setIconImage(Toolkit.getDefaultToolkit().getImage(RegFrm.class.getResource("/images/图书馆s.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 626, 481);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblNewLabel = new JLabel("新用户注册");
		lblNewLabel.setIcon(new ImageIcon(RegFrm.class.getResource("/images/头像.png")));
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 28));
		
		JLabel lblNewLabel_1 = new JLabel("用户名");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 23));
		
		JLabel lblNewLabel_2 = new JLabel("密码");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 23));
		
		JLabel lblNewLabel_3 = new JLabel("出生日期");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 23));
		
		userNametxt = new JTextField();
		userNametxt.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		
		JButton btnNewButton = new JButton("确定注册");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				resetValueActionPerformed(e);
			}//确定注册事件
		});
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 23));
		
		passwordtxt = new JPasswordField();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap(176, Short.MAX_VALUE)
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE)
					.addGap(171))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(86)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(lblNewLabel_2)
						.addComponent(lblNewLabel_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(lblNewLabel_3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addComponent(passwordtxt, GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
						.addComponent(textField_2)
						.addComponent(userNametxt, GroupLayout.PREFERRED_SIZE, 269, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(145, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(228)
					.addComponent(btnNewButton)
					.addContainerGap(245, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(39)
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
					.addGap(36)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(userNametxt, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_2)
						.addComponent(passwordtxt, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
					.addGap(30)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE))
					.addGap(32)
					.addComponent(btnNewButton)
					.addContainerGap(35, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
		this.setLocationRelativeTo(null);//设置页面居中
	}

	/**
	 * 注册事件处理
	 * @param e
	 */
	
	protected void actionPerformed(ActionEvent e) {
		userAddActionPerformed(e);
		
//		String name=this.userNametxt.getText();
//		String password=this.passwordtxt.getText();
//		if(StringUtil.isEmpty(password)) {
//			JOptionPane.showMessageDialog(null, "密码不能为空！");
//			return;
//		}// TODO Auto-generated method stub
//		BookType bookType=new BookType(name,password);//封装实体
//        Connection con=null;
//        try {
//        	con=dbUtil.getCon();
//        	int n=userDao.add(con, username);
//        	
//        	if(n==1) {
//        		
//        		JOptionPane.showMessageDialog(null, "图书类别添加成功");
//        		resetValue();
//        		
//        	}else {
//        		JOptionPane.showMessageDialog(null, "图书类别添加失败！");
//        	}
//        }catch(Exception e) {
//        	e.printStackTrace();
//        	JOptionPane.showMessageDialog(null, "图书类别添加失败！");
//        	
//        }finally {
//        	try {
//        		
//        	
//        	dbUtil.closeCon(con);
//        	} catch(Exception e) {
//        		e.printStackTrace();
//        	}}
		}
	/**
	 * 注册事件处理
	 * @param e
	 */
	private void userAddActionPerformed(ActionEvent e) {
		String username=this.userNametxt.getText();
		String password=this.passwordtxt.getText();
		if(StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null," 密码不能为空");
			return;
		}// TODO Auto-generated method stub
		User userName=new User(username,password);
		Connection con=null;
		try {
			con=dbUtil.getCon();
        	int n=userDao.add(con, userName);
		}catch(Exception evt) {
			evt.printStackTrace();
		}finally {
			try{
				DBUtils.closeCon(con);
			}catch(Exception evt) {
				
			}
			
		}
		
	}

	/**
	 * 
	 * 重置事件处理
	 * @param e
	 */
	private void resetValueActionPerformed(ActionEvent evt) {
		this.resetValue();// TODO Auto-generated method stub
		
	}

	/**
	 * 
	 */
	private void resetValue() {
		this.userNametxt.setText("");
		this.passwordtxt.setText("");// TODO Auto-generated method stub
		
	}

	
}
